package com.consul.consul_consumer.controller;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

/**
 * @author xiaojf
 */
@RestController
public class ConsumerController {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private DiscoveryClient discoveryClient;


    @GetMapping("consumer-test")
    public String consumer() {
        List<ServiceInstance> instances = discoveryClient.getInstances("producer");
        ServiceInstance serviceInstance = instances.get(0);
        URI uri = serviceInstance.getUri();
        String tageUrl = uri + "/producer-test";
        String str = restTemplate.getForObject(tageUrl, String.class);
        return str;
    }
}
